<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/5/18
 * Time: 上午9:52
 */

class Salesman_stat_model extends Base_Model
{
    private $_appid;
    private $_appsecret;
    private $_wx_type;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->database();
    }

    private function getSourceConfig($source = ''){
        $this->config->load('weixin');
        if (!empty($source)){
            $this->_appid = $this->config->item($source.'_appid');
            $this->_appsecret = $this->config->item($source.'_appsecret');
            $this->_wx_type = $this->config->item($source.'_type');
        }else{
            //默认能量逗服务号
            $this->_appid = $this->config->item('nldou_fuwu_appid');
            $this->_appsecret = $this->config->item('nldou_fuwu_appsecret');
            $this->_wx_type = $this->config->item('nldou_fuwu_type');
        }
    }

    public function createUser($wxtype='',$openid)
    {
        //微信接口获取用户信息
        $this->getSourceConfig($wxtype);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->getUserInfo($openid);

//        echo '3--------'.PHP_EOL;
//        var_dump($res);echo PHP_EOL;

        //未关注公号用户
//        array(4) {
//        ["subscribe"]=>int(0)
//        ["openid"]=>"oHlcTt7k0U-w5QaTvRqqvDQvfOQQ"
//        ["unionid"]=>"oOYHo1F1_Y9_tfeGc6oMgzKPOjps"
//        ["tagid_list"]=>array(0) {}
//        }
        //接口获取失败
        if (!isset($res['openid'])||!isset($res['unionid'])) return false;
        //获取成功，注册用户
        $data = array(
            'utype'      => 'weixin',
            'wx_openid'  => $res['openid'],
            'wx_unionid' => $res['unionid'],
            'userName'   => isset($res['nickname'])?$res['nickname']:'',
            'userToken'  => md5(time().uniqid()),
            'userIcon'   => isset($res['headimgurl'])?$res['headimgurl']:'',
            'status'     => 1,
            'created'    => time(),
            'changed'    => time()
        );

//        echo '4--------'.PHP_EOL;
//        var_dump($data);echo PHP_EOL;

        $uid = $this->user_model->createUser($data);

//        echo '5--------'.PHP_EOL;
//        var_dump($uid);echo PHP_EOL;

        if($uid){
            $this->user_model->createUserInfo(array('uid' => $uid));
        }
        return $uid;
    }

    public function getUidByOpenid($openid)
    {
        return $this->user_model->getUser(array('wx_openid'=>$openid),'uid');
    }

    public function savePostStat($data)
    {
        if($this->db->insert('mod_slspost_stat', $data)){
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function saveArticleStat($data)
    {
        if($this->db->insert('mod_slsarticle_stat', $data)){
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function sychArticleStat()
    {
        $this->load->model('redis_model');
        $redis = $this->redis_model->getRedisInstance();

        $articles = $this->db->select('model')->from('mod_salesman_article')->get()->result_array();

        $offset = 0;
        while (true){
            //遍历代言人
            $index = $offset+1;//代言人序号
            $sls = $this->db->select('idcode')->from('mod_salesman')->order_by('id','DESC')->limit(10,$offset)->get()->result_array();
            if (empty($sls)) break;
            //每次10个代言人
            foreach ($sls as $sl){
                $readnum = 0;
                //每个代言人遍历文章
                foreach ($articles as $a){
                    $randkey = $redis->get('salesman.article.reverse.'.$a['model'].$sl['idcode']);
                    //有分享文章
                    if ($randkey!==false){
                        $readnum += $redis->bitCount('salesman.article.read.'.$randkey);
                    }
                }
                //有阅读数的存入
                if ($readnum>0){
                    $redis->set('slsartv2.readtotal.'.$sl['idcode'],$readnum);
                }
                echo $index.' '.$sl['idcode'].' sharenum '.$readnum.PHP_EOL;
                $index++;
            }
            $offset+=10;
        }
    }
}